package com.example.base.model;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.util.DateUtils;
import com.example.base.SpringContextHolder;
import com.example.base.Cache.CacheKey;

import org.hibernate.SessionFactory;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.metadata.ClassMetadata;

import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.util.Date;

@MappedSuperclass
public class BaseEntity implements CacheKey {

    public static final Integer DELETE = 1;

    public static final Integer NORMAL = 0;

    @Id//主键
    @GeneratedValue//自增长
    private Long id;//主键id


    private Integer updater;// 更新人


    private Date updateTime;// 更新时间


    private Integer creater;// 创建者


    private Date createTime;// 创建时间


    private Integer recStatus = 0;// 是否删除（0：未删除 1：已删除）


    public Long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }





    public Integer getUpdater() {

        return updater;
    }

    public void setUpdater(Integer updater) {

        this.updater = updater;
    }

    public Date getUpdateTime() {

        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {

        this.updateTime = updateTime;
    }

    public Integer getCreater() {

        return creater;
    }

    public void setCreater(Integer creater) {

        this.creater = creater;
    }

    public Date getCreateTime() {

        return createTime;
    }

    public void setCreateTime(Date createTime) {

        this.createTime = createTime;
    }

    public Integer getRecStatus() {

        return recStatus;
    }

    public void setRecStatus(Integer recStatus) {

        this.recStatus = recStatus;
    }

    public String getStrCreateTime() {

        return DateUtils.format19(this.createTime);
    }
    @Override
    public String cacheKey() {
        return this.getClass().getSimpleName()+":"+getId().toString();
    }
    public String[] metaInfo()
    {
        EntityManagerFactory entityManagerFactory = SpringContextHolder.getBean(EntityManagerFactory.class);
        SessionFactoryImpl sessionFactory = (SessionFactoryImpl) entityManagerFactory.unwrap(SessionFactory.class);
        ClassMetadata metadata=sessionFactory.getClassMetadata(this.getClass());
        return metadata.getPropertyNames();
    }
    @Override
     public String toString()
    {
        SerializeWriter out = new SerializeWriter();
        try {
            JSONSerializer serializer = new JSONSerializer(out);
            serializer.config(SerializerFeature.SkipTransientField, false);
            serializer.config(SerializerFeature.WriteClassName, true);
            serializer.write(this);
            return out.toString();
        } finally {
            out.close();
        }

    }

    /**
     EntityManagerFactory entityManagerFactory = SpringContextHolder.getBean(EntityManagerFactory.class);
     SessionFactoryImpl sessionFactory = (SessionFactoryImpl) entityManagerFactory.unwrap(SessionFactory.class);
     StringBuffer buffer=new StringBuffer();
     ClassMetadata metadata=sessionFactory.getClassMetadata(User.class);
     String []minfo=metadata.getPropertyNames();
     Map<String, EntityPersister> persisterMap = sessionFactory.getEntityPersisters();
     for (Map.Entry<String, EntityPersister> entity : persisterMap.entrySet()) {
     Class targetClass = entity.getValue().getMappedClass();
     SingleTableEntityPersister persister = (SingleTableEntityPersister) entity.getValue();
     Iterable<AttributeDefinition> attributes = persister.getAttributes();
     String entityName = targetClass.getSimpleName();//Entity的名称
     String tableName = persister.getTableName();//Entity对应的表的英文名
     buffer.append("类名：" + entityName + " => 表名：" + tableName+" ");
     System.out.println("类名：" + entityName + " => 表名：" + tableName);

     //属性
     for (AttributeDefinition attr : attributes) {
     String propertyName = attr.getName(); //在entity中的属性名称
     String[] columnName = persister.getPropertyColumnNames(propertyName); //对应数据库表中的字段名
     String type = "";
     PropertyDescriptor targetPd = BeanUtils.getPropertyDescriptor(targetClass, propertyName);
     if (targetPd != null) {
     type = targetPd.getPropertyType().getSimpleName();
     }
     buffer.append( type +"：" +propertyName + "：" + ":" + columnName[0]);
     }

     }
    */
}
